#include "BaseType.h"
#include "TraceDebug.h"
#include "ComPort.h"
#include "Thread.h"

#define COM_PORT	"/dev/ttySAC0"

class ComReciever:public RunnableProtocol{
public:
	void run()
	{
		sint32 ret;
		ComPort* pCom = new ComPort();
		pCom->setAttribute(COM_ATTR_BAUDRATE, 115200);
		ret=pCom->open(COM_PORT, IO_MODE_RDWR_ONLY);
		if(STATUS_ERROR==ret)
		{
			TRACE_ERR("open %s error:%s\n", COM_PORT, ERROR_STRING);
			return;
		}
		while(1)
		{
			char hello[]="hello ComPort...\r\n";
			ret=pCom->writeData(hello, sizeof(hello));
			if(ret<=0)
			{
				TRACE_ERR("Com recv error,ret=%d\n",ret);
			}
			sleep(5);
		}

	}
};


void TestComPort()
{
	sint32 ret;
	Thread recvThread;
	ComReciever comRecv;
	recvThread.start(&comRecv);

	ComPort* pCom = new ComPort();
	pCom->setAttribute(COM_ATTR_BAUDRATE, 115200);
	ret=pCom->open(COM_PORT, IO_MODE_RDWR_ONLY);
	if(STATUS_ERROR==ret)
	{
		TRACE_ERR("open %s error:%s\n", COM_PORT, ERROR_STRING);
		return;
	}
	
	while(1)
	{
		char buf[64]={0};
		ret=pCom->readData(buf, 63);
		if(ret>0)
		{
			TRACE_DEBUG("COM recv:[%s]\n",buf);
		}
		else
		{
			TRACE_ERR("Com recv error,ret=%d\n",ret);
			pCom->writeData("Com recv error.\n", 16);
			sleep(1);
		}
	}
}